package org.yihang.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.DigestUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.yihang.entity.Condition;
import org.yihang.entity.Hero;
import org.yihang.service.HeroService;
import org.yihang.service.KungfuService;
import org.yihang.service.MenpaiService;
import org.yihang.service.WeaponService;

@RestController
@CrossOrigin
public class HeroController {

	@Autowired
	private HeroService heroService;

	@Autowired
	private MenpaiService menpaiService;

	@Autowired
	private KungfuService kungfuService;

	@Autowired
	private WeaponService weaponService;

	@PutMapping(path = "hero")
	public Map<String, Object> save(@RequestBody Hero hero) {
		heroService.save(hero);
		Map<String, Object> map = new HashMap<>();
		map.put("success", true);
		return map;
	}

	@PostMapping(path = "hero/{id}")
	public Map<String, Object> update(@PathVariable int id, @RequestBody Hero hero) {
		hero.setId(id);
		heroService.update(hero);
		Map<String, Object> map = new HashMap<>();
		map.put("success", true);
		return map;
	}

	@DeleteMapping(path = "hero/{id}")
	public Map<String, Object> delete(@PathVariable int id) {
		heroService.delete(id);
		Map<String, Object> map = new HashMap<>();
		map.put("success", true);
		return map;
	}

	@GetMapping(path = "hero/{id}")
	public Map<String, Object> findById(@PathVariable int id) {
		Map<String, Object> map = new HashMap<>();
		map.put("success", true);
		map.put("hero", heroService.findById(id));
		return map;
	}

	@GetMapping(path = "hero")
	public Map<String, Object> findByCondition(Condition condition) {
		Map<String, Object> map = new HashMap<>();
		map.put("success", true);
		map.put("list", heroService.findByCondition(condition));
		map.put("total", heroService.findCount(condition));
		return map;
	}

	@GetMapping(path = "menpai")
	public Map<String, Object> findMenpai() {
		Map<String, Object> map = new HashMap<>();
		map.put("success", true);
		map.put("map", menpaiService.findAll());
		return map;
	}

	@GetMapping(path = "weapon")
	public Map<String, Object> findWeapon() {
		Map<String, Object> map = new HashMap<>();
		map.put("success", true);
		map.put("map", weaponService.findAll());
		return map;
	}

	@GetMapping(path = "kungfu")
	public Map<String, Object> findKungfu() {
		Map<String, Object> map = new HashMap<>();
		map.put("success", true);
		map.put("map", kungfuService.findAll());
		return map;
	}

	@PostMapping(path = "upload/{type}")
	public Map<String, Object> upload(@PathVariable String type,InputStream in) throws IOException {
		Map<String, Object> map = new HashMap<>();		
		if(!"jpg".equals(type) && !"png".equals(type)) {
			map.put("exception", "您提供的请求参数格式有误:仅可以是jpg或png");
			map.put("success", false);
			return map;
		}
		
		byte[] bytes = FileCopyUtils.copyToByteArray(in);
		String filename = DigestUtils.md5DigestAsHex(bytes)+"."+type;
		FileCopyUtils.copy(bytes, new FileOutputStream("d:\\upload\\"+filename));
		
		
		map.put("success", true);
		map.put("filename", filename);
		return map;
	}

	@RequestMapping("400")
	@ResponseStatus(code=HttpStatus.BAD_REQUEST)
	public Map<String, Object> handler400() {
		Map<String, Object> map = new HashMap<>();
		map.put("success", false);
		map.put("exception", "您提供的请求参数格式有误");
		return map;
	}

	@RequestMapping("403")
	@ResponseStatus(code=HttpStatus.FORBIDDEN)
	public Map<String, Object> handler403() {
		Map<String, Object> map = new HashMap<>();
		map.put("success", false);
		map.put("exception", "您没有相应的权限");
		return map;
	}

	@RequestMapping("404")
	@ResponseStatus(code=HttpStatus.NOT_FOUND)
	public Map<String, Object> handler404() {
		Map<String, Object> map = new HashMap<>();
		map.put("success", false);
		map.put("exception", "您需要的资源不存在");
		return map;
	}

	@RequestMapping("500")
	@ResponseStatus(code=HttpStatus.INTERNAL_SERVER_ERROR)
	public Map<String, Object> handler500() {
		Map<String, Object> map = new HashMap<>();
		map.put("success", false);
		map.put("exception", "您需要的资源出现内部错误");
		return map;
	}

}
